/*
 * 创建日期 2009-12-14
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.grid.bean;

import  org.fantasy.common.db.bean.ReportRow;
import  org.fantasy.common.grid.report.ReportDataParser;
import  org.fantasy.common.grid.util.GridUtil;
import  org.fantasy.common.util.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 报表参数
 * @author: 王文成
 * @version: 1.0
 * @since 2009-12-14
 */
public class ReportTagParam extends BaseParam implements ReportParam {

    private final Log log = LogFactory.getLog(ReportTagParam.class);

    public static final String REPORT_TYPE_STAT = "STAT";

    public static final String REPORT_TYPE_LIST = "LIST";

    private String statCols;

    private String sumCols;

    private String subTotal;

    private String sumTotal;

    protected String reportType;

    public ReportTagParam() {
        log.debug(">>>>>>>>>>>>>>>>>>> New ReportTagParam instance!");
    }

    /**
     * 初始化配置
     * 
     * @throws Exception
     */
    @Override
    public void prepare() throws Exception {
        if (this.prepare)
            return;
        super.prepare();
    }

    public void setReportType(String reportType) {
        this.reportType = reportType;
    }

    @Override
    @SuppressWarnings("all")
    protected List<ReportRow> formatDataList() throws Exception {
        List dataList = super.formatDataList();
        if (dataList.isEmpty())
            return new ArrayList(0);
        Object o = dataList.get(0);
        if (o instanceof DynaBean) {
            // 兼容DynaBean格式的查询结果
            dataList = GridUtil.dynaBeanList2Report(dataList);
        }
        // 如果是统计型报表进行数据转换
        if (REPORT_TYPE_STAT.equals(reportType)) {
            ReportDataParser parser = new ReportDataParser();
            parser.setDataList(dataList);
            parser.setStatCols(getStatColsList());
            parser.setSumCols(getSumColsList());
            parser.setSumTotal(isSumTotal());
            parser.setSubTotal(isSubTotal());
            parser.parse();
            return parser.getResultList();
        }
        return (List<ReportRow>) dataList;
    }

    public boolean isStatReport() {
        return StringUtil.isValid(sumCols) && StringUtil.isValid(statCols);
    }
    
    public String getSumCols() throws Exception{
        return StringUtil.jion(getSumColsList(),",");
    }

    public String getStatCols() throws Exception{
        return StringUtil.jion(getStatColsList(),",");
    }
    
    /**
    * 是否小计
    * @return
    * @throws Exception
     */
    public boolean isSubTotal() throws Exception {
        if (StringUtil.isValid(subTotal)) {
            return "true".equals(subTotal);
        } else {
            return "true".equals(table.getSubTotal());
        }
    }
    
    /**
    * 是否合计
    * @return
    * @throws Exception
     */
    public boolean isSumTotal() throws Exception {
        if (StringUtil.isValid(sumTotal)) {
            return "true".equals(sumTotal);
        } else {
            return "true".equals(table.getSumTotal());
        }
    }
    
    /**
    * 维度列
    * @return
    * @throws Exception
     */
    public List<String> getStatColsList() throws Exception {
        if (StringUtil.isValid(statCols)) {
            return Arrays.asList(statCols.split(","));
        } else {
            return table.getStatCols();
        }
    }
    
    /**
    * 指标列
    * @return
    * @throws Exception
     */
    public List<String> getSumColsList() throws Exception {
        if (StringUtil.isValid(sumCols)) {
            return Arrays.asList(sumCols.split(","));
        } else {
            return table.getSumCols();
        }
    }

    public void setSumCols(String sumCols) {
        this.sumCols = sumCols;
    }

    public void setStatCols(String statCols) {
        this.statCols = statCols;
    }

    public void setSubTotal(String subTotal) {
        this.subTotal = subTotal;
    }

    public void setSumTotal(String sumTotal) {
        this.sumTotal = sumTotal;
    }

    @Override
    public String toString() {
        JSONObject json = new JSONObject();
        json.put("var", var);
        json.put("tableConfig", tableConfig);
        json.put("page", page);
        json.put("action", action);
        json.put("style", style);
        json.put("statCols", statCols);
        json.put("sumCols", sumCols);
        json.put("subTotal", subTotal);
        json.put("sumTotal", sumTotal);
        json.put("rowCustom", rowCustom);
        json.put("cellCustom", cellCustom);
        json.put("reportType", reportType);
        json.put("exportName", exportName);
        json.put("identity", identity);
        return json.toString();
    }
}